package net.apps123.view;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;

import net.apps123.soundmeter.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

public class HunXiangAdapter extends BaseAdapter {

    private Context context;
    private List<Integer> datas = new ArrayList<>();
    HashMap<String, List<Integer>> hunXiangMap;
    private List<String> keyList = new ArrayList<>();


    public HunXiangAdapter(Context context, List<Integer> datas, HashMap<String, List<Integer>> hunXiangMap) {
        this.context = context;
        this.datas = datas;
        this.hunXiangMap = hunXiangMap;

        Set<String> keys = hunXiangMap.keySet();
        keyList = new ArrayList<>();
        keyList.add("100");
        keyList.add("125");
        keyList.add("160");
        keyList.add("200");
        keyList.add("250");
        keyList.add("315");
        keyList.add("400");
        keyList.add("500");
        keyList.add("630");
        keyList.add("800");
        keyList.add("1000");
        keyList.add("1250");
        keyList.add("2000");
        keyList.add("2500");
        keyList.add("3150");
        keyList.add("4000");
        keyList.add("5000");

        List<Integer> dataList = new ArrayList<>();
        dataList.add(86);
        dataList.add(83);
        dataList.add(87);
        dataList.add(86);
        dataList.add(92);
        dataList.add(90);
        dataList.add(90);
        dataList.add(90);
        dataList.add(90);
        dataList.add(90);
        dataList.add(90);
        dataList.add(90);
        dataList.add(87);
        dataList.add(84);
        dataList.add(81);
        dataList.add(82);
        dataList.add(75);
        dataList.add(72);
        dataList.add(70);
        dataList.add(64);
        dataList.add(66);
        dataList.add(61);
        dataList.add(63);
        dataList.add(60);
        dataList.add(54);
        dataList.add(55);
        dataList.add(50);
        dataList.add(43);
        dataList.add(46);
        dataList.add(51);
        dataList.add(52);
        dataList.add(39);
        this.datas = dataList;


    }

    @Override
    public int getCount() {
        if (keyList != null) {
            return keyList.size();
        }
        return 0;
    }

    @Override
    public String getItem(int position) {
        return keyList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(context).inflate(R.layout.adapter_hunxiang_layout, parent, false);
        LineChart lineChart = (LineChart) convertView.findViewById(R.id.lineChart);


//        Short[] arr = datas.get(position).toArray(new Short[datas.get(position).size()]);
//        short[] updateData = new short[arr.length];
//        for (int i=0; i<arr.length; i++){
//            updateData[i] = arr[i];
//        }
//        holder.splView.updateView( datas.get(position).getData(), datas.get(position).getIpStr() );
//        setData( holder.lineChart, datas);
        setData2(lineChart,  this.datas, keyList, position);

        return convertView;
    }

//    private class ViewHolder {
//        LineChart lineChart;
//    }

    public void setData(LineChart lineChart, List<Integer> datas, List<String> keyList) {
//        float datas[] = {14f, 15f, 16f, 17f, 16f, 16f};
        //在MPAndroidChart一般都是通过List<Entry>对象来装数据的
        List<Entry> entries = new ArrayList<Entry>();
        //循环取出数据
        for (int i = 0; i < datas.size(); i++) {
            entries.add(new Entry(i, datas.get(i)));
        }
        //一个LineDataSet对象就是一条曲线
        LineDataSet lineDataSet = new LineDataSet(entries, "第一条数据");
        //LineData才是正真给LineChart的数据
//        LineData lineData = new LineData(lineDataSet);
//        if (lineChart != null){
//            lineChart.setData(lineData);
//        }

    }

    private void setData2(LineChart lineChart, List<Integer> datas, List<String> keyList, int position) {
        if (datas == null) {
            return;
        }

        ArrayList<String> xVals = new ArrayList<String>();
        for (int i = 0; i < datas.size(); i++) {
            xVals.add((i * 20) + "ms");
        }

        ArrayList<Entry> yVals1 = new ArrayList<Entry>();

        for (int i = 0; i < datas.size(); i++) {

            float val = (float) (datas.get(i));// + (float)
            // ((mult *
            // 0.1) / 10);
            yVals1.add(new Entry(val, i));
        }


        LineDataSet set1;

        if (lineChart.getData() != null &&
                lineChart.getData().getDataSetCount() > 0) {
            set1 = (LineDataSet) lineChart.getData().getDataSetByIndex(0);
            set1.setYVals(yVals1);
            lineChart.getData().setXVals(xVals);
            lineChart.getData().notifyDataChanged();
            lineChart.notifyDataSetChanged();
        } else {
            // create a dataset and give it a type
            if (keyList != null && position < keyList.size()) {
                set1 = new LineDataSet(yVals1, "" + keyList.get(position));
            } else {
                set1 = new LineDataSet(yVals1, "DataSet 1");
            }


            set1.setAxisDependency(YAxis.AxisDependency.LEFT);
            set1.setColor(ColorTemplate.getHoloBlue());
//            set1.setCircleColor(Color.WHITE);
            set1.setLineWidth(2f);
//            set1.setCircleRadius(3f);
            set1.setFillAlpha(65);
            set1.setFillColor(ColorTemplate.getHoloBlue());
            set1.setHighLightColor(Color.rgb(244, 117, 117));
            set1.setDrawCircleHole(false);
            //set1.setFillFormatter(new MyFillFormatter(0f));
            //set1.setDrawHorizontalHighlightIndicator(false);
            //set1.setVisible(false);
            //set1.setCircleHoleColor(Color.WHITE);


            ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
            dataSets.add(set1); // add the datasets

            // create a data object with the datasets
            LineData data = new LineData(xVals, dataSets);
            data.setValueTextColor(Color.WHITE);
            data.setValueTextSize(9f);

            // set data
            lineChart.setData(data);
            lineChart.setTouchEnabled(false);
            lineChart.setScaleEnabled(false);
            lineChart.setDragEnabled(false);
            lineChart.setScaleXEnabled(false);
            lineChart.setScaleYEnabled(false);
        }
    }


}
